package com.bilibili.bilibili.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bilibili.bilibili.R;
import com.bilibili.bilibili.adapter.TabLayoutAdapter;
import com.bilibili.bilibili.customview.CircleImageView;
import com.bilibili.bilibili.customview.NoScrollViewPager;
import com.bilibili.bilibili.fragment.BangumiFragment;
import com.bilibili.bilibili.fragment.DiscoverFragment;
import com.bilibili.bilibili.fragment.LiveFragment;
import com.bilibili.bilibili.fragment.RecommendFragment;
import com.bilibili.bilibili.fragment.RegionFragment;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    private NoScrollViewPager mViewpager;
    private List<Fragment> mFragments;
    private SlidingTabLayout mTablayout;
    private View mView;
    private CircleImageView mBackground_image_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mFragments = new ArrayList<>();
        BangumiFragment bangumiFragment = new BangumiFragment();
        DiscoverFragment discoverFragment = new DiscoverFragment();
        LiveFragment liveFragment = new LiveFragment();
        RecommendFragment recommendFragment = new RecommendFragment();
        RegionFragment regionFragment = new RegionFragment();
        mFragments.add(liveFragment);
        mFragments.add(recommendFragment);
        mFragments.add(bangumiFragment);
        mFragments.add(regionFragment);
        mFragments.add(discoverFragment);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mTablayout = (SlidingTabLayout) findViewById(R.id.tablayout);
        mViewpager = (NoScrollViewPager) findViewById(R.id.viewpager);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        mView = navigationView.inflateHeaderView(R.layout.nav_header_main);
        mBackground_image_view = (CircleImageView) mView.findViewById(R.id.user_avatar_view);
        mBackground_image_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        TabLayoutAdapter tabLayoutAdapter = new TabLayoutAdapter(getSupportFragmentManager(), mFragments);
        mViewpager.setOffscreenPageLimit(5);
        mViewpager.setAdapter(tabLayoutAdapter);
        mTablayout.setViewPager(mViewpager);
        mViewpager.setCurrentItem(1);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.item_home) {
            //主页
            Toast.makeText(this, "主页", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.item_vip) {
            //会员
            Toast.makeText(this, "会员", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.item_download) {
            //下载
            Toast.makeText(this, "下载", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.item_favourite) {
            //收藏

        } else if (id == R.id.item_history) {
            //历史记录

        } else if (id == R.id.item_group) {
            //关注的人

        } else if (id == R.id.item_tracker) {
            //我的钱包

        } else if (id == R.id.item_theme) {
            //主题

        } else if (id == R.id.item_app) {
            //应用推荐

        } else if (id == R.id.item_settings) {
            //设置与帮助

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
